//
// Complex.js：复数类
//

class Complex {
  constructor(re = 0, im = 0) {
    this.re = re;
    this.im = im;
  }

  getReal() {
    return this.re;
  }

  getImage() {
    return this.im;
  }

  add(c) {
    const that = this;
    const tmp = new Complex(that.re + c.re, that.im + c.im);
    return tmp;
  }

  sub(c) {
    const that = this;
    const tmp = new Complex(that.re - c.re, that.im - c.im);
    return tmp;
  }

  mul(c) {
    const that = this;
    const tmp = new Complex(that.re * c.re - that.im * c.im, that.re * c.im + that.im * c.re);
    return tmp;
  }

  div(co) {
    const a = this.re; const b = this.im; const c = co.re; const
      d = co.im;
    const tmp = new Complex((a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
    return tmp;
  }

  mod2() {
    const a = this.re; const
      b = this.im;
    return a * a + b * b;
  }
}

export default Complex;
